--- 战斗主类
---
--- @class FighterAttributeManager:AbstractFighterAttributeManager
FighterAttributeManager = ClientFight.CreateClass("FighterAttributeManager", ClientFight.AbstractFighterAttributeManager)
local this = FighterAttributeManager


---@type BuffAttributeCalculator
this.buffAttributeCalculator = BuffAttributeCalculator.New();
---@type TeamFighterCalculator
this.teamFighterCalculator = TeamFighterCalculator.New();
---@type ProfessionAdditionCalculator
this.professionAdditionCalculator = ProfessionAdditionCalculator.New();
function this.initFighterAttrCal()
    this.registerFighterAttrCal(this.buffAttributeCalculator);
    this.registerFighterAttrCal(this.professionAdditionCalculator);
    this.registerTeamFighterAttrCal(this.teamFighterCalculator);
end

addOverload(this, "getFightAttr", function( fighter, type)
    -- 战斗属性
    return this.getFightAttr(fighter, type.id);
end, "table", "table")

--[[
* @param id    属性id
*/]]
addOverload(this, "getFightAttr", function( fighter, id)
    local obj = fighter.totalAttribute.attributes[id];
    if (obj == nil) then
        return 0;
    end
    -- 战斗属性
    return obj;
end, "table",  "number")

--[[
* 100% 克隆所有属性
* @param source
* @param target
* @param excludes
*/]]
addOverload(this, "cloneAllAttr", function(source, target, excludes)
    for key, entry in pairs(source.attributes) do

        local is = true;
        for i, v in pairs(excludes) do
            if key == v then
                is = false;
            end
        end
        if is then
            local attr = entry;
            local cloneAttr = BaseAttribute.New();
            target.attributes[key] = cloneAttr;
            -- 克隆全部属性
            for i, attrEnum in pairs(FighterAttributeEnum:values()) do

                local srcValue = attr.attributes[attrEnum.id];
                if (srcValue ~= 0) then
                    cloneAttr.attributes[attrEnum.id] = srcValue;
                end
            end
        end
    end
end, "table", "table", "table")

--[[
* 克隆全部属性
* @param source    属性来许愿
* @param target
* @param attrPercent 继承属性与百分比  [[属性id,万分比],[属性id,万分比]...]
*/]]
addOverload(this, "cloneAllAttr", function(source, target, excludes, attrPercent)
    local map = {};
    for i, ints in pairs(attrPercent) do
        map[ints[1]] = ints[2];
    end

    for key, entry in pairs(source.attributes) do
        local is = true;
        for i, v in pairs(excludes) do
            if key == v then
                is = false;
            end
        end
        if is then
            local attr = entry;
            local cloneAttr = BaseAttribute.New();
            target.attributes[key] = cloneAttr;
            -- 克隆全部属性
            for i, attrEnum in pairs(FighterAttributeEnum:values()) do
                if map[i] then
                    local attrVal = this.getMapValue(attr.attributes, attrEnum.id) * map[attrEnum.id] or 0 / Global.TEN_THOUSANDTH;
                    cloneAttr.attributes[attrEnum.id] = attrVal;
                end
            end
        end
    end
end, "table", "table", "table", "table")

function this.countFighterAttribute(fight, fighter, type)
    super.countFighterAttribute(fight, fighter, type);
    local maxHp = this.getFightAttr(fighter, FighterAttributeEnum.HP);
    if (fighter.hp > maxHp) then
        fighter:setHp(maxHp);
    end
end
this.initFighterAttrCal();